<html>
<head><meta charset="utf-8"><title>static pointers · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html">static pointers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185211034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185211034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185211034">(Jan 09 2020 at 13:21)</a>:</h4>
<p>Hi,<br>
why is <code>static A: *const u8 = &5;</code> not allowed but <code> static A: &amp;u8 = &5;</code> is?<br>
also, why is a non mut static pointer "not thread safe"? it's read only memory into a static variable. it should be pretty defined.</p>



<a name="185214650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185214650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185214650">(Jan 09 2020 at 14:11)</a>:</h4>
<p>I think I understand.<br>
<code>static A: &amp;u8 = &5;</code> is a reference so there's a promise there cannot safely be a <code>&amp;mut T</code> here. but <code>*const T</code> doesn't have such promise</p>



<a name="185219123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185219123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185219123">(Jan 09 2020 at 14:58)</a>:</h4>
<p>you can't go from a <code>*const T</code> to a <code>&amp;mut T</code> in safe code</p>



<a name="185219171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185219171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185219171">(Jan 09 2020 at 14:59)</a>:</h4>
<p>IIRC there is an issue about implementing Send and Sync for raw pointers in the rfc repo</p>



<a name="185691062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185691062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185691062">(Jan 15 2020 at 12:03)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span>  the technical answer is that statics must be <code>Sync</code>, and raw pointers are not</p>



<a name="185696816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185696816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185696816">(Jan 15 2020 at 13:27)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> the unanswered part of the question is why aren't raw pointers <code>Sync</code>, where the current answer is probably "because that would be a footgun"</p>



<a name="185696921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185696921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185696921">(Jan 15 2020 at 13:28)</a>:</h4>
<p>this protection is not strictly necessary, since to trigger undefined UB one still needs to use <code>unsafe</code> code to read/write through these pointers.</p>



<a name="185696942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185696942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185696942">(Jan 15 2020 at 13:29)</a>:</h4>
<p>and there one is already proving that there are no data-races</p>



<a name="185713838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185713838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185713838">(Jan 15 2020 at 16:19)</a>:</h4>
<p>indeed, raw pointers are non-<code>Sync</code> mostly as a lint... and because a <code>struct</code> containing raw pointers probably has some sufficiently interesting stuff going on that users should explicitly acknowledge thread-safety with <code>unsafe impl Send/Sync</code></p>



<a name="185713907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185713907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185713907">(Jan 15 2020 at 16:20)</a>:</h4>
<p>(also they can't opt-out by themselves as <code>impl !Send/Sync</code> is unstable)</p>



<a name="185714011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714011">(Jan 15 2020 at 16:21)</a>:</h4>
<p>for <code>static</code>s that a bit silly, but then what is the use-case for a <code>static</code> const ptr anyway?</p>



<a name="185714396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714396">(Jan 15 2020 at 16:24)</a>:</h4>
<blockquote>
<p>for statics that a bit silly, but then what is the use-case for a static const ptr anyway</p>
</blockquote>
<p>mapping a static to a dynamic array?</p>



<a name="185714419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714419">(Jan 15 2020 at 16:24)</a>:</h4>
<p>e.g. the linker puts a size at a symbol, and the array content at another symbol</p>



<a name="185714479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714479">(Jan 15 2020 at 16:25)</a>:</h4>
<p>so you end up with:</p>
<div class="codehilite"><pre><span></span><span class="k">extern</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">ARRAY_SIZE</span>: <span class="kt">usize</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">ARRAY</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="185714588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714588">(Jan 15 2020 at 16:26)</a>:</h4>
<p>You can't make <code>ARRAY</code> an array cause you don't know the length within your Rust program, You can't make it a reference cause maybe the size is <code>0</code>  (and other issues)</p>



<a name="185714600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185714600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185714600">(Jan 15 2020 at 16:26)</a>:</h4>
<p>You also can't make it a slice</p>



<a name="185729376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185729376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185729376">(Jan 15 2020 at 18:39)</a>:</h4>
<p>ah... interesting. doesnt match the OP, but still a good answer.</p>



<a name="185814533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185814533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185814533">(Jan 16 2020 at 13:33)</a>:</h4>
<blockquote>
<p>for <code>static</code>s that a bit silly, but then what is the use-case for a <code>static</code> const ptr anyway?</p>
</blockquote>
<p>My use case is passing that to FFI, by default it uses an extern static for a pointer, I wanted to replace that in fuzzing with a null pointer</p>



<a name="185840159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185840159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185840159">(Jan 16 2020 at 17:47)</a>:</h4>
<p>I dont know what "using an extern static for a pointer" means</p>



<a name="185911580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185911580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185911580">(Jan 17 2020 at 12:36)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> if FFI uses a raw pointer, you should use a raw pointer as well</p>



<a name="185911666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185911666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185911666">(Jan 17 2020 at 12:36)</a>:</h4>
<p>If you want to access that from multiple threads, then you should make sure that this is safe, and provide an abstraction (using <code>unsafe</code> code) that does that.</p>



<a name="185990878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185990878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185990878">(Jan 18 2020 at 11:09)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> well the only reason I opened this is <em>because</em> my FFI uses raw pointer but rust doesn't allow me to define a raw pointer static myself.</p>



<a name="185990931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185990931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185990931">(Jan 18 2020 at 11:10)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> I'd expect <code>extern { static Foo: UnsafeCell&lt;*const c_int&gt;; }</code> to just work</p>



<a name="185990937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185990937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185990937">(Jan 18 2020 at 11:11)</a>:</h4>
<p>but yeah, because raw pointers aren't <code>Sync</code>, you can't use bare raw pointers</p>



<a name="185990942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185990942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185990942">(Jan 18 2020 at 11:11)</a>:</h4>
<p>alternatively you can have a <code>#[repr(transparent)] struct Wrapper(*const c_int);</code> that implements <code>Sync</code>.</p>



<a name="185990995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static%20pointers/near/185990995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/static.20pointers.html#185990995">(Jan 18 2020 at 11:12)</a>:</h4>
<p>:/ that sucks. Too bad we can't override this with unsafe somehow. But I guess that's the language :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>